/* The code examples copyrighted by Nokia Corporation that are included to
 * this material are licensed to you under following MIT-style License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0

Page {
    id: calendarViewPage
    anchors.margins: 10
    anchors.fill: parent

    QueryDialog {
        id: errorDialog
        titleText: "Error"
        message: "Failed to remove event"
        acceptButtonText: "Ok"
    }

    Column {
        spacing:  10

        Grid {
            columns: 2
            spacing: 10
            Button {
                id: addEventButton
                text: "Add Event"
                width: (calendarViewPage.width - 30)/2
                onClicked: {
                    calendarViewPage.openFile("CalendarAddEvent.qml")
                }
            }
            Button {
                id: removeEventButton
                text: "Remove Event"
                width: (calendarViewPage.width - 30)/2
                onClicked: {
                    if (CalendarExample.RemoveEvent(eventsView.currentIndex)) {
                    }
                    else {
                        errorDialog.open()
                    }
                }
            }
            Text {text: "<b>Events for day: </b>"; font.pointSize: 17}
            Button {
                id: viewDateButton
                text: "Click to set"
                width: (calendarViewPage.width - 30)/2
                onClicked: {
                    dateDialog.open()
                }
            }
        }

        ListView {
            id: eventsView
            model: CalendarModel
            width: parent.width
            height: calendarViewPage.height - y - 20
            clip: true
            delegate: Text {
                text: eventstarttime + "-" + eventendtime + " " + eventsummary + " (" + eventlocation + ")"
                width:  parent.width
                font.pointSize: 17
                MouseArea { anchors.fill: parent; onClicked:eventsView.currentIndex = index }
            }
            highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
            highlightFollowsCurrentItem: true
        }
    }

    function setViewDate() {
        viewDateButton.text = dateDialog.year + "-" + (dateDialog.month < 10 ? "0" : "" ) + dateDialog.month + "-" + (dateDialog.day < 10 ? "0" : "" ) + dateDialog.day
        CalendarExample.SetDate(viewDateButton.text)

    }

    DatePickerDialog {
        id: dateDialog
        titleText: "Select date"
        onAccepted: setViewDate()
    }

    function openFile(file) {
        var component = Qt.createComponent(file)

        if (component.status == Component.Ready)
            pageStack.push(component);
        else
            console.log("Error loading component:", component.errorString());
    }
}
